package cn.edu.niit.server.service.impl;

import cn.edu.niit.server.domain.ContentInfo;
import cn.edu.niit.server.repository.ContentInfoRepository;
import cn.edu.niit.server.service.ContentInfoService;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link ContentInfo}.
 */
@Service
@Transactional
public class ContentInfoServiceImpl implements ContentInfoService {

    private final Logger log = LoggerFactory.getLogger(ContentInfoServiceImpl.class);

    private final ContentInfoRepository contentInfoRepository;

    public ContentInfoServiceImpl(ContentInfoRepository contentInfoRepository) {
        this.contentInfoRepository = contentInfoRepository;
    }

    @Override
    public ContentInfo save(ContentInfo contentInfo) {
        log.debug("Request to save ContentInfo : {}", contentInfo);
        return contentInfoRepository.save(contentInfo);
    }

    @Override
    public Optional<ContentInfo> partialUpdate(ContentInfo contentInfo) {
        log.debug("Request to partially update ContentInfo : {}", contentInfo);

        return contentInfoRepository
            .findById(contentInfo.getId())
            .map(
                existingContentInfo -> {
                    if (contentInfo.getContentAuthor() != null) {
                        existingContentInfo.setContentAuthor(contentInfo.getContentAuthor());
                    }
                    if (contentInfo.getContentText() != null) {
                        existingContentInfo.setContentText(contentInfo.getContentText());
                    }
                    if (contentInfo.getContentImg() != null) {
                        existingContentInfo.setContentImg(contentInfo.getContentImg());
                    }
                    if (contentInfo.getContentTime() != null) {
                        existingContentInfo.setContentTime(contentInfo.getContentTime());
                    }
                    if (contentInfo.getContentPraiseCount() != null) {
                        existingContentInfo.setContentPraiseCount(contentInfo.getContentPraiseCount());
                    }
                    if (contentInfo.getContentFavorateCount() != null) {
                        existingContentInfo.setContentFavorateCount(contentInfo.getContentFavorateCount());
                    }
                    if (contentInfo.getContentImgLabel() != null) {
                        existingContentInfo.setContentImgLabel(contentInfo.getContentImgLabel());
                    }

                    return existingContentInfo;
                }
            )
            .map(contentInfoRepository::save);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<ContentInfo> findAll(Pageable pageable) {
        log.debug("Request to get all ContentInfos");
        return contentInfoRepository.findAll(pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<ContentInfo> findOne(Long id) {
        log.debug("Request to get ContentInfo : {}", id);
        return contentInfoRepository.findById(id);
    }

    @Override
    public void delete(Long id) {
        log.debug("Request to delete ContentInfo : {}", id);
        contentInfoRepository.deleteById(id);
    }

    @Override
    public Page<ContentInfo> findByType(Long typeId, Integer page, Integer size) {
        return contentInfoRepository.getContentInfoByType(typeId, PageRequest.of(page, size));
    }

    @Override
    public Page<ContentInfo> getMyContent(String login, Integer page, Integer size) {
        return contentInfoRepository.getMyContent(login, PageRequest.of(page, size));
    }
}
